<?php
class GalleriesController extends AppController {

	var $name = 'Galleries';
	var $uses = array('Gallery');
	function beforeFilter() {
		parent::beforeFilter();
		$this->set('mod','galleries');
	}
	function admin_index() {
		$this->Gallery->recursive = 0;
		$key = $this->data['Gallery']['key'];
		$status = $this->data['Gallery']['status'];
		if($key!= null){
			$conditions = array('Gallery.name LIKE '=>'%'.$key.'%');
		}elseif($status != null){
			$conditions = array('Gallery.status' => $status);
		}
		elseif($status != null && $key!= null){
			$conditions = array('Gallery.status' => $status,'Gallery.name LIKE '=>'%'.$key.'%');
		}else{
			$conditions = array();
		}
		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Gallery.id DESC'
		);
		$this->set('galleries', $this->paginate());
		$this->set(compact('key','status'));
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại thư viện ảnh này!', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('gallery', $this->Gallery->read(null, $id));
		$this->paginate = $this->Gallery->Img->find('all',array(
				'fields'=>array('Img.id','Img.url'),
				'conditions'=>array('Img.galleries_id'=>$id)
			));
		$this->set('images',$this->paginate());
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Gallery->create();
			if ($this->Gallery->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu !', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Có lỗi sảy ra. Hãy thử lại !', true));
			}
		}
 	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại thư viện ảnh này !', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Gallery->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu !', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Có lỗi sảy ra. Hãy thử lại !', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Gallery->read(null, $id);
		}
 		$this->set('model',$this->uses[0]);

	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại thư viện ảnh này !', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Gallery->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Có lỗi sảy ra. Hãy thử lại !', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi sảy ra. Hãy thử lại !', true));
    		$this->redirect(array('action' => 'index'));
        }
         foreach($arrid as $item){
    		  $this->Gallery->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index'));
    }

    function index($id = null)
	{
		$this->Gallery->recursive = 1;
		$conds= array('Gallery.status'=>2);
		$this->paginate = array(
			'conditions'=>$conds,
			'limit'=>30,
			'order'=>'Gallery.id DESC'
		);
		$this->set('galleries', $this->paginate());
		if($id !=null){
			$images = $this->Gallery->Imgs->find('all',array('conditions'=>array('Imgs.galleries_id'=>$id)));
			$galley = $this->Gallery->find('first',array('conditions'=>array('Gallery.id'=>$id)));
			$this->set(compact('images','galley'));

		}
	}
	function view($id = NULL)
	{
 		$this->Img->recursive = 1;
		if (!$id) {
			$this->Session->setFlash(__('Bạn chưa chọn thư viện ảnh nào !', true));
			$this->redirect(array('controller'=>'galleries','action' => 'index'));
		}
        if($id !=NULL)
	   {
   		/*$conds= array('Imgs.galleries_id'=>$id);
			$this->paginate = array(
			'conditions'=>$conds,
			'limit'=>12,
			'order'=>'Imgs.id DESC'
		);*/
		$images = $this->Gallery->Img->find('all',array('conditions'=>array('Img.galleries_id'=>$id)));
 		$galley = $this->Gallery->find('first',array('conditions'=>array('Gallery.id'=>$id)));

 		$others_galley = $this->Gallery->find('list',array('conditions'=>array('Gallery.id <>'=>$id,'Gallery.status'=>2),'limit'=>10,'order'=>'Gallery.id DESC'));
 		$this->set(compact('images','galley','others_galley'));

		}
  	}

}
?>